package gherkinedit.templatesystem;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Template {
	private String name;
	// private List<TemplatePart> partList;
	public String templateString;

	public Template(String name, String templateString) {
		this.name = name;
		this.templateString = templateString;
	}

	public List<String> getTextInputList() {
		ArrayList<String> textInputs = new ArrayList<String>();

		Pattern regexPattern = Pattern.compile("\\([^\\)]+\\)", Pattern.UNICODE_CASE | Pattern.CANON_EQ);
		Matcher matcher = regexPattern.matcher(this.templateString);
		while (matcher.find()) {
			textInputs.add(matcher.group());
		}

		return textInputs;
	}

	public String getName() {
		return new String(name);
	}

	public static ClassLoader getClassLoader() {
		Template dummy = new Template("dummy", "dummy");
		return dummy.getClass().getClassLoader();
	}

	public String getResultingTemplateString(List<String> userInputs) {
		Pattern regexPattern = Pattern.compile("\\([^\\)]+\\)", Pattern.UNICODE_CASE | Pattern.CANON_EQ);
		Matcher matcher = regexPattern.matcher(this.templateString);
		StringBuffer sb = new StringBuffer();
		for (int i=0; matcher.find(); i++) {
			matcher.appendReplacement(sb, Matcher.quoteReplacement(userInputs.get(i)));
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
}
